import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:travel/model/hotel.dart';
import 'package:travel/model/message.dart';
import 'package:travel/routes/index.dart';
import 'package:travel/screen/flutter_screenutil.dart';
import 'package:travel/ui/draw_under_line.dart';
import 'package:travel/ui/scroll_refresh_config.dart';
import 'package:travel/utils/colors.dart';
import 'package:travel/utils/styles.dart';

class OrderController extends GetxController {
  RefreshController refreshController = RefreshController();

  RxList<MessageOrderModel> pageList = RxList([]);

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    pageList.add(MessageOrderModel(
        addTime: '2023-11-25 11:42:05',
        title: '订单支付提醒',
        info: '故宫博物馆成人票，尚未完成支付，即将取消。',
        orderNo: '231125173310080'));

    /// 完毕
    refreshController.loadComplete();
  }

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
  }

  void initData() {
    pageList.add(MessageOrderModel(
        addTime: '2023-11-25 11:42:05',
        title: '订单支付提醒',
        info: '故宫博物馆成人票，尚未完成支付，即将取消。',
        orderNo: '231125173310080'));
    pageList.add(MessageOrderModel(
        addTime: '2023-11-25 11:42:05',
        title: '订单预订成功',
        info: '故宫博物馆成人票*1份，2023-11-26 待使用。',
        orderNo: '231125173310080'));
    pageList.add(MessageOrderModel(
        addTime: '2023-11-25 11:42:05',
        title: '订单退订成功',
        info: '故宫博物馆成人票，退订金额：￥50.00（含保险金额：￥10.00），将于1-7个工作日退至原支付账户。',
        orderNo: '231125173310080'));
    pageList.add(MessageOrderModel(
        addTime: '2023-11-25 11:42:05',
        title: '订单已使用',
        info: '故宫博物馆成人票*1份，2023-11-20 已使用。欢迎再次预订。',
        orderNo: '231125173310080'));
  }
}

class OrderView extends GetView<OrderController> {
  const OrderView({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF7f7),
          appBar: AppBar(
            automaticallyImplyLeading: true,
            backgroundColor: Colors.white,
            leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Icon(
                Icons.arrow_back_ios,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.color3333),
                size: 20.w,
              ),
            ),
            leadingWidth: 40.w,
            title: Text(
              '订单消息',
              style: StylesUtils.customTextStyle(fontSize: 18.sp, color: ColorsUtils.color0000),
            ),
          ),
          body: scrollRefreshConfig(context),
        ));
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (ctx, index) {
        var row = controller.pageList[index];
        return Column(
          children: [
            Container(
              height: 40.h,
              alignment: Alignment.center,
              child: Text(
                row.addTime,
                style: StylesUtils.customTextStyle(color: ColorsUtils.color9999, fontSize: 12.sp),
              ),
            ),
            Center(
              child: Container(
                width: 345.w,
                decoration:
                    BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.w)),
                padding: EdgeInsets.all(10.w),
                child: Column(
                  children: [
                    Container(
                      height: 30.h,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        row.title,
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color0000, fontSize: 15.sp),
                      ),
                    ),
                    DrawUnderLine(
                      color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5),
                      child: Container(
                        padding: EdgeInsets.only(bottom: 10.h),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          row.info,
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color6666, fontSize: 13.sp, lineHeight: 1.2.h),
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 30.h,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '订单：${row.orderNo}',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color6666, fontSize: 12.sp),
                          ),
                          InkWell(
                              onTap: () {
                                AdmissionOrderStatusEnum e = AdmissionOrderStatusEnum.values
                                    .firstWhere((element) => element.value == index + 1,
                                        orElse: () => AdmissionOrderStatusEnum.unpaid);
                                Get.toNamed(RoutePath.orderAdmissionInfo,
                                    arguments: {'admission': e});
                              },
                              child: Text.rich(TextSpan(children: [
                                TextSpan(
                                    text: '查看详情 ',
                                    style: StylesUtils.customTextStyle(
                                        color: ColorsUtils.color6799, fontSize: 12.sp)),
                                WidgetSpan(
                                    alignment: PlaceholderAlignment.middle,
                                    child: Icon(
                                      CupertinoIcons.right_chevron,
                                      color: ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color9999),
                                      size: 12.sp,
                                    ))
                              ])))
                        ],
                      ),
                    )
                  ],
                ),
              ),
            )
          ],
        );
      },
      itemCount: controller.pageList.length,
    );
  }
}
